package com.dq.controller.api.small;

import com.dq.exception.customException.ApiException;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.goods.HotSearchGoods;
import com.dq.domain.org.Org;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author heshuaijun
 * @date 2019/9/1613:44
 */
@Api(description = "轮播管理")
@RestController
@RequestMapping("/api/hotSearch")
public class HotSearchGoodsController extends AllService {
    @ApiOperation("所有热门检索列表")
    @PostMapping("/list/{orgId}")
    public VO list(@PathVariable @ApiParam("门店id") Long orgId) throws ApiException {
        Org org = dao.get(Org.class, orgId);
        if (org == null) {
            throw new ApiException("未找到门店");
        }
        List<HotSearchGoods> hotSearchGoods = hotSearchGoodsRepository.findByOrgAndDeletedFalseOrderBySearchNumDesc(org);
        return new Vo(VO.toVOCollection(hotSearchGoods, false));
    }

    @ApiOperation("新增热门检索列表")
    @PostMapping("/add/{orgId}/{searchName}")
    public VO add(@PathVariable @ApiParam("检索词") String searchName) throws
            ApiException {
        return new Vo(addHot(searchName));
    }

    public HotSearchGoods addHot( String searchName) {
        HotSearchGoods hotSearchGoods = hotSearchGoodsRepository.getByNameAndDeletedFalse(searchName);
        if (hotSearchGoods == null) {
            hotSearchGoods = new HotSearchGoods();
            hotSearchGoods.setName(searchName);
            hotSearchGoods.setSearchNum(1l);
        } else {
            hotSearchGoods.setSearchNum(hotSearchGoods.getSearchNum() + 1);
        }
        return hotSearchGoodsRepository.save(hotSearchGoods);
    }
}
